package com.bugreport.utils;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

public class MessageFactory implements InitializingBean {

	private static final Logger logger = Logger.getLogger(MessageFactory.class);

	private static MessageFactory singleton;
	private ResourceBundle bundle;

	public void afterPropertiesSet() throws Exception {
		singleton = this;
	}

	public void setMessageBundle(ResourceBundle bundle) {
		this.bundle = bundle;
	}

	public String getMessage(String key) {
		return bundle.getString(key);
	}

	public String getMessage(String key, String... args) {
		return MessageFormat.format(bundle.getString(key), (Object[]) args);
	}

	public static MessageFactory getInstance() {

		if (singleton == null) {
			logger.fatal("MessageFactory has not been well initialized:");
			logger.fatal("MessageFactory singleton is null.");
			logger.fatal("You should not see this error in production version of the application. Please report issue to administrator");
		}

		return singleton;
	}
}
